package com.ecoolex.discount.sales.core.manager;

import com.ecoolex.discount.sales.core.dao.YouWorldGiftCardOrderDAO;
import com.ecoolex.discount.sales.core.external.pojo.YouWorldOrder;
import com.ecoolex.discount.sales.entity.YouWorldGiftCardOrder;
import com.ecoolex.discount.sales.entity.YouWorldGiftCardOrder.Column;
import com.ecoolex.discount.sales.entity.YouWorldGiftCardOrderExample;
import com.ecoolex.discount.sales.pojo.QueryYouWorldGiftCardOrderDTO;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

import static com.ecoolex.discount.sales.entity.YouWorldGiftCardOrder.NOT_DELETED;

@Service
public class YouWorldGiftCardOrderManager {
    @Autowired
    private YouWorldGiftCardOrderDAO dao;

    public void save(YouWorldOrder result, String createUser) {
        YouWorldGiftCardOrder order = new YouWorldGiftCardOrder();
        order.setOrderId(result.getOrder_id());
        order.setFaceValue(result.getFace_value());
        order.setCostPoint(result.getCost_point());
        order.setCardId(result.getCard_id());
        order.setOrderStatus(result.getOrder_status());
        order.setCardName(result.getCard_name());
        order.setCardBarcodeImg(result.getCard_barcode_img());
        order.setCardBarcodeNumber(result.getCard_barcode_number());
        order.setCardPinNumber(result.getCard_pin_number());
        order.setCardCertificateLink(result.getCard_certificate_link());
        order.setCardLogoUrl(result.getCard_logo_url());
        order.setCardImageUrl(result.getCard_image_url());
        order.setCardDiscountPercent(result.getCard_discount_percent());
        order.setCardDiscountUsd(result.getCard_discount_usd());
        order.setDeleted(NOT_DELETED);
        order.setCreateUser(createUser);
        order.setCreateTime(Date.from(Instant.now()));
        order.setUpdateUser(createUser);
        order.setUpdateTime(Date.from(Instant.now()));
        dao.insert(order);
    }
    
    public YouWorldGiftCardOrder getByOrderId(String orderId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(orderId, YouWorldGiftCardOrder.NOT_DELETED);
    }

    public Pagination<YouWorldGiftCardOrder> query(QueryYouWorldGiftCardOrderDTO query, Sorting[] sorts, Page page) {
        YouWorldGiftCardOrderExample example = new YouWorldGiftCardOrderExample();
        YouWorldGiftCardOrderExample.Criteria criteria = example.createCriteria();

        if (Check.notNullOrTrimEmpty(query.getOrderId())) {
            criteria.andOrderIdEqualTo(query.getOrderId());
        }
        if (Check.notNull(query.getCardId())) {
            criteria.andCardIdEqualTo(query.getCardId());
        }
        if (Check.notNullOrTrimEmpty(query.getCardBarcodeNumber())) {
            criteria.andCardBarcodeNumberEqualTo(query.getCardBarcodeNumber());
        }

        criteria.andDeletedEqualTo(NOT_DELETED);

        example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toString());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }
}